package com.lagou.flashsale.dao.impl;

import com.lagou.flashsale.dao.OrderDAO;
import com.lagou.flashsale.entity.Order;
import com.lagou.flashsale.entity.OrderStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class OrderDAOImpl implements OrderDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override//将订单写入到数据库中
    public void insert(Order order) {
        jdbcTemplate.update("insert into tb_order(order_id, user_id, order_status, content) values (?, ?, ?, ?)",
                order.getOrderId(), order.getUserId(), order.getOrderStatus().getStatus(), order.getContent());
    }

    @Override//更新库存信息
    public boolean update(Order order) {

        Integer status = jdbcTemplate.queryForObject("select order_status from tb_order where order_id=?",
                new String[]{order.getOrderId()}, Integer.class);

        // 如果已支付，则不更新
        if (status != OrderStatus.PAYED.getStatus()) {

            //更新记录取消订单
            jdbcTemplate.update("update tb_order set order_status=? where order_id=?",
                    order.getOrderStatus().getStatus(), order.getOrderId());
            return true;
        }
        return false;
    }
}
